package com.zjq.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sun.org.apache.xpath.internal.operations.Mod;
import com.zjq.pojo.Author;
import com.zjq.pojo.BookKind;
import com.zjq.pojo.Books;
import com.zjq.pojo.Comments;
import com.zjq.service.BookService;
import com.zjq.service.BookStoreService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import sun.misc.Request;


import javax.annotation.Resource;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("BookServiceImpl")
    private BookService bookService;


    @Autowired
    @Qualifier("BookStoreServiceImpl")
    private BookStoreService bookStoreService;

    /*查询全部书籍*/
   /* @RequestMapping("/allBooks")
    public String list(Model model){
        List<Books> list = bookService.queryAllBook();
        model.addAttribute("list",list);
        return "allbook";
    }*/

    /*添加书籍*/
    @RequestMapping("/toAddBook")
    public String toAddPaper(Model model){
        List<BookKind> bookKinds = bookService.queryBookKind();
        model.addAttribute("bookKinds",bookKinds);
        return "backstage/book/addBook";
    }

    @RequestMapping("/addBook")
    @ResponseBody
    public String addBook(Books books, MultipartFile file ,Author author) throws IOException {
        List<Author> authors = bookService.queryAuthorByName(author.getAuthorName());
        if (authors.size() == 0){
            bookService.addAuthor(author);
        }
        System.out.println("有没有进来@！！！！！！！！！！！！！！！！！！！！！");
        String filename = "/images/"+file.getOriginalFilename();
        books.setBookImg(filename);
        String url = "D:\\bookImg";
        File file1 = new File(url, file.getOriginalFilename());
        System.out.println("创建文件");
        file.transferTo(file1);
        System.out.println("存入文件");
        System.out.println("================="+filename);
        bookService.addBook(books);
        int pn = bookService.queryAllBook().size()/ 5 + 1;
        return "<script>window.location.href='allBook?pn="+pn+"'</script>";
    }

    /*修改书籍*/
    @RequestMapping("/toUpdateBook")
    public String toUpdatePaper(int id,Model model){
        Books books = bookService.queryBookById(id);
        model.addAttribute("book",books);
        List<BookKind> bookKinds = bookService.queryBookKind();
        model.addAttribute("bookKinds",bookKinds);
        return "backstage/book/updateBook";
    }
    @RequestMapping("/updateBook")
    public String updateBook(Books book){
        System.out.println("===================="+book);
        bookService.updateBook(book);
        return "redirect:/book/allBook";
    }

    /*删除书籍*/
    @RequestMapping("/deletePaper")
    @ResponseBody
    public String deleteBook(int id,int pn){
        bookService.deleteBookById(id);
        return "<script>window.location.href='allBook?pn="+pn+"'</script>";
    }

    /*模糊查询*/
    @RequestMapping("/queryBookByName")
    public String queryBookByName(String name,@RequestParam(value = "pn", required = true, defaultValue = "1") Integer pn, HttpServletRequest request, Model model){
        PageHelper.startPage(pn, 5);
        //startPage后面紧跟查询就是一个分页查询。
        List<Books> list = bookService.queryBookByName(name);
        //使用PageInfo包装查询后的结果，只需要将PageInfo交给页面就行。
        //封装了详细的分页信息，包括我们查询出来的数据userList，传入连续显示的页数5。
        PageInfo<Books> page = new PageInfo<Books>(list, 5);
        model.addAttribute("pageInfo", page);
        model.addAttribute("key",name);
        return "backstage/book/allbook";
    }

    /*分页*/
    @RequestMapping("/allBook")
    public String bookList(@RequestParam(value = "pn", required = true, defaultValue = "1") Integer pn, HttpServletRequest request, Model model){
        PageHelper.startPage(pn, 5);
        //startPage后面紧跟查询就是一个分页查询。
        List<Books> list = bookService.queryAllBook();
        List<BookKind> bookKinds = bookService.queryBookKind();
        //使用PageInfo包装查询后的结果，只需要将PageInfo交给页面就行。
        //封装了详细的分页信息，包括我们查询出来的数据userList，传入连续显示的页数5。
        PageInfo<Books> page = new PageInfo<Books>(list, 5);
        model.addAttribute("pageInfo", page);
        model.addAttribute("bookKind",bookKinds);
        return "backstage/book/allbook";
    }

    /*添加分类*/
    @RequestMapping("/toaddBookKind")
    public String toaddBookKind(){
        return "backstage/book/addBookKind";
    }

    @RequestMapping("/addBookKind")
    public String addBookKind(BookKind bookKind){
        bookService.addBookKind(bookKind);
        return "redirect:/book/toAddBook";
    }

    /*分类查询*/
    @RequestMapping("/queryBookByKindId")
    public String queryBookByKindId(int bookkindId,@RequestParam(value = "pn", required = true, defaultValue = "1") Integer pn, HttpServletRequest request, Model model){
        PageHelper.startPage(pn, 5);
        List<Books> list = bookService.queryBookByKindId(bookkindId);
        PageInfo<Books> page = new PageInfo<Books>(list, 5);
        List<BookKind> bookKinds = bookService.queryBookKind();
        model.addAttribute("pageInfo", page);
        model.addAttribute("bookKind",bookKinds);
        return "backstage/book/allbook";
    }

    @RequestMapping("/addSession")
    public void addSession(String key, HttpSession session){
        session.setAttribute("key",key);
    }


    @RequestMapping("/queryAuthorByName")
    @ResponseBody
    public List<Author> queryAuthorByName(String name){
        List<Author> authors = bookService.queryAuthorByName(name);
        return authors;
    }

    @RequestMapping("/queryAllComment")
    public String queryAllComment(Model model,@RequestParam(value = "pn", required = true, defaultValue = "1") Integer pn){
        PageHelper.startPage(pn,8);
        List<Comments> comments = bookStoreService.queryAllComment();
        PageInfo<Comments> commentsPageInfo = new PageInfo<Comments>(comments);
        model.addAttribute("pageInfo",commentsPageInfo);
        return "backstage/book/queryAllComment";
    }
}
